package
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class GradMap extends Sprite
	{
		public static const GRAD_WIDTH:int = 40;
		
		public static const GRAD_HEIGTH:int = 40;
		
		public var line:Array=[];
		
		private  var barrier:Array = [];
		
		private var map:Object={};
		
		private var _isCanClick:Boolean=true;
		
		private var _isShow:Boolean = true;
		public function GradMap()
		{
			super();
			initGrad();
			this.addEventListener(MouseEvent.CLICK,mouse_click);
			this.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
		}

		protected function mouse_up(event:MouseEvent):void
		{
			var grad:Grad = event.target as  Grad;
			TowerWarMap.instance.plantView.createPlant(grad.x,grad.y);
		}
		
		public function initline():void{
			var loader:URLLoader = new URLLoader();
			loader.load(new URLRequest("resource/"+TowerWarMap.level+".xml"));
			loader.addEventListener(Event.COMPLETE,initConfig);
		}
		
		public function drawPlantGrad(p:Point):void{
			var key:String = (p.x - GRAD_WIDTH/2)+"_"+(p.y - GRAD_WIDTH/2);
			if(map[key]){
				var grad:Grad = map[key];
				grad.isCanSit = false;
				grad.graphics.clear();
				grad.graphics.lineStyle(2,0xffffff);
				grad.graphics.beginFill(0x00ff00);
				grad.graphics.drawRect(0,0,GRAD_WIDTH - 2,GRAD_HEIGTH-2);
				grad.graphics.endFill();
			}
		}
		
		protected function initConfig(event:Event):void
		{
			line.length = 0;
			var xml:XML = XML(event.target.data);
			var xmlList:XMLList = xml.line.children();
			var p:Point;
		    var xxx:XML;
			for each(xxx in xmlList){
				p = new Point(xxx.@x,xxx.@y);
				line.push(p);
			}
			TowerWarMap.instance.background.initface();
			drawLine();

		    xmlList = xml.barrier.children();
			for each(xxx in xmlList){
				p = new Point(xxx.@x,xxx.@y);
				barrier.push(p);
			}
			drawBarrier();
			
			xmlList = xml.plants.children();
			for each(xxx in xmlList){
				TowerWarMap.instance.plantView.initPlantData(xxx);
			}
		}
		
		private function drawBarrier():void
		{
			for(var i:int=0;i<barrier.length;i++){
				var p:Point = barrier[i];
				var key:String = (p.x - GRAD_WIDTH/2)+"_"+(p.y - GRAD_WIDTH/2);
				if(map[key]){
					var grad:Grad = map[key];
					grad.isCanSit = false;
					grad.graphics.clear();
					grad.graphics.lineStyle(2,0xffffff);
					grad.graphics.beginFill(0xffff00);
					grad.graphics.drawRect(0,0,GRAD_WIDTH - 2,GRAD_HEIGTH-2);
					grad.graphics.endFill();
				}
			}
		}
		
		private function drawLine():void
		{
			for(var i:int=0;i<line.length;i++){
				var p:Point = line[i];
				var key:String = (p.x - GRAD_WIDTH/2)+"_"+(p.y - GRAD_WIDTH/2);
				if(map[key]){
					var grad:Grad = map[key];
					grad.isCanSit = false;
					grad.graphics.clear();
					grad.graphics.lineStyle(2,0xffffff);
					grad.graphics.beginFill(0xffff00);
					grad.graphics.drawRect(0,0,GRAD_WIDTH - 2,GRAD_HEIGTH-2);
					grad.graphics.endFill();
				}
			}
			line.length = 0;
		}
		
		protected function mouse_click(event:MouseEvent):void
		{
			var grad:Grad = event.target as Grad;

			if(grad.isCanSit == true){
				if(EditPlantPanel.getInstance().targetPlant){
					EditPlantPanel.getInstance().targetPlant.gradAry.push(grad.point);
				}else{
					line.push(grad.point);
				}
				grad.isCanSit = false;
				grad.graphics.clear();
				grad.graphics.lineStyle(2,0xffffff);
				grad.graphics.beginFill(0xffff00);
				grad.graphics.drawRect(0,0,GRAD_WIDTH - 2,GRAD_HEIGTH-2);
				grad.graphics.endFill();
			}else{
				if(EditPlantPanel.getInstance().targetPlant){
					EditPlantPanel.getInstance().targetPlant.gradAry.splice(EditPlantPanel.getInstance().targetPlant.gradAry.indexOf(grad.point),1);
				}else{
					line.splice(line.indexOf(grad.point),1)
				}
				grad.graphics.clear();
				grad.graphics.lineStyle(2,0x000000);
				grad.graphics.beginFill(0xffff00,0);
				grad.graphics.drawRect(0,0,GRAD_WIDTH - 2,GRAD_HEIGTH-2);
				grad.graphics.endFill();
				grad.isCanSit = true;
			}
			
		}
		
		private function initGrad():void
		{
			//行
			var col:int = Math.ceil(TowerWarMap.instance.HEIGHT/GRAD_HEIGTH);
			//列
			var row:int = Math.ceil(TowerWarMap.instance.WIDTH/GRAD_WIDTH);
			for(var i:int=0;i<row;i++){
				for(var j:int=0;j<col;j++){
					var grad:Grad = new Grad();
					grad.graphics.lineStyle(2,0x000000);
					grad.graphics.beginFill(0xffff00,0);
					grad.graphics.drawRect(0,0,GRAD_WIDTH - 2,GRAD_HEIGTH-2);
					grad.graphics.endFill();
					grad.x = i*GRAD_WIDTH;
					grad.y = j*GRAD_HEIGTH;
					grad.name=i+"_"+j;
					grad.isCanSit = true;
					this.addChild(grad); 
					grad.point = new Point(grad.x + GRAD_WIDTH/2,grad.y + GRAD_HEIGTH/2);
					map[(i*GRAD_WIDTH)+"_"+(j*GRAD_HEIGTH)] = grad;
				}
			}
			
		}
		
		public function set isCanClick(value:Boolean):void
		{
			_isCanClick = value; 
			if(value){
				this.mouseChildren = true;
				this.mouseEnabled = true;
			}else{
				this.mouseChildren = false;
				this.mouseEnabled = false;
			}
		}

		public function get isCanClick():Boolean
		{
			return _isCanClick;
		}

		public function isShow(e:MouseEvent):void
		{
			_isShow = !_isShow;
			_isShow ? this.visible = true : this.visible = false;
		}
	}
}